﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using NHibernate.Cfg;
using NHibernate.Context;
using OS.Infrastructure.NHibernate;
using TaskKernel.Domain.Nhibernate.Mappings;

namespace TaskKernel.Domain.Nhibernate
{
    public class NHInitializer : INHibernateInitializer
    {
        private readonly string _connectionString;

        public NHInitializer(string connectionString)
        {
            _connectionString = connectionString;
        }

        public Configuration GetConfiguration()
        {
            MsSqlConfiguration persistenceConfigurer = MsSqlConfiguration
                    .MsSql2008
                 //   .ConnectionString(connectionStringBuilder => connectionStringBuilder.FromConnectionStringWithKey("ApplicationServices"))
                    .ConnectionString(_connectionString)
                    .UseReflectionOptimizer()
                    .ShowSql();

            FluentConfiguration cfg = Fluently.Configure()
                    .CurrentSessionContext<ThreadStaticSessionContext>()
                    .Database(persistenceConfigurer)
                    .Mappings(m => m.FluentMappings.AddFromAssemblyOf<TaskMap>());
            return cfg.BuildConfiguration();
        }

    }
}
